package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @PROJECT_NAME:sky-take-out
 * @NAME: AddressBookServicelmpl
 * @AUTHOR:$$
 * @Date:2023-08-19
 * @DESCRIPTION:
 **/
@Service
@Slf4j
public class AddressBookServicelmpl  implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 条件查询
     * @param addressBook
     * @return
     */
    @Override
    public List<AddressBookMapper> list(AddressBook addressBook) {
        return addressBookMapper.list(addressBook);
    }

    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
     addressBook.setIsDefault(0);
     addressBookMapper.insert(addressBook);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public AddressBook geById(Long id) {
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
   addressBookMapper.upadate(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    @Transactional
    public void setDefault(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ?when user_id
     addressBook.setIsDefault(0);
     addressBook.setUserId(BaseContext.getCurrentId());
     addressBookMapper.updateIsDefaultByUserId(addressBook);
       //2、将当前地址修改为默认地址 update address_book set is_default = ?when id = ？
        addressBook.setIsDefault(1);
        addressBookMapper.upadate(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void deletById(Long id) {

    }
}
